package com.yanqu.road.dao.impl.activity.pawnshop;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.pawnshop.PawnshopTriggerData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PawnshopTriggerDaoImpl extends TempDataObjectDao<PawnshopTriggerData> {
    public static String getPawnshopTriggerDataSql = "select * from `t_u_activity_pawnshop_trigger` where `activityId`=?";

    public Map<Integer, PawnshopTriggerData> getPawnshopTriggerData(long activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, PawnshopTriggerData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getPawnshopTriggerDataSql);
            int i = 1;
            call.setLong(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                PawnshopTriggerData data = parse(rs);
                dataMap.put(data.getId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updatePawnshopTriggerDataSql = "update `t_u_activity_pawnshop_trigger` set isUse=?,triggerTime=?,useTime=? where id=? and userId=? and activityId = ?";

    @Override
    protected boolean updateData(PawnshopTriggerData pawnshopTriggerData) {
        pawnshopTriggerData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BOOLEAN, pawnshopTriggerData.isUse()));
        para.put(i++, new DbParameter(Types.BIGINT, pawnshopTriggerData.getTriggerTime()));
        para.put(i++, new DbParameter(Types.BIGINT, pawnshopTriggerData.getUseTime()));
        para.put(i++, new DbParameter(Types.INTEGER, pawnshopTriggerData.getId()));
        para.put(i++, new DbParameter(Types.BIGINT, pawnshopTriggerData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, pawnshopTriggerData.getActivityId()));
        if (getDb().executeNoneQuery(updatePawnshopTriggerDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertPawnshopTriggerDataSql = "insert into `t_u_activity_pawnshop_trigger`(id,userId,isUse,triggerTime,useTime,activityId) values (?,?,?,?,?,?)";

    @Override
    protected boolean addData(PawnshopTriggerData pawnshopTriggerData) {
        pawnshopTriggerData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, pawnshopTriggerData.getId()));
        para.put(i++, new DbParameter(Types.BIGINT, pawnshopTriggerData.getUserId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, pawnshopTriggerData.isUse()));
        para.put(i++, new DbParameter(Types.BIGINT, pawnshopTriggerData.getTriggerTime()));
        para.put(i++, new DbParameter(Types.BIGINT, pawnshopTriggerData.getUseTime()));
        para.put(i++, new DbParameter(Types.INTEGER, pawnshopTriggerData.getActivityId()));
        if (getDb().executeNoneQuery(insertPawnshopTriggerDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected PawnshopTriggerData parseData(ResultSet rs) throws SQLException {
        PawnshopTriggerData pawnshopTriggerData = new PawnshopTriggerData();
        pawnshopTriggerData.setId(rs.getInt("id"));
        pawnshopTriggerData.setUserId(rs.getLong("userId"));
        pawnshopTriggerData.setUse(rs.getBoolean("isUse"));
        pawnshopTriggerData.setTriggerTime(rs.getLong("triggerTime"));
        pawnshopTriggerData.setUseTime(rs.getLong("useTime"));
        pawnshopTriggerData.setActivityId(rs.getInt("activityId"));
        return pawnshopTriggerData;
    }

}
